package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.dao.ChatMessage;

import java.util.List;


public class Popup_Msg_Operate implements View.OnClickListener
{
	private PopupWindow m_popup;

	private OnDismissListener m_dismissListener;

	private Context mContext;

	private TextView m_TvCopy, m_TvDelete, m_TvForward,m_TvReply,m_TvReport;

	private View m_DeleteContainer, m_CopyContainer, m_ForwardContainer, m_ReplyContainer, m_ReportContainer;

	private IIMSelectorMenu m_listener;

	private ChatMessage m_msg;

	public Popup_Msg_Operate(Context pContext, IIMSelectorMenu listenerr, OnDismissListener dismissListener)
	{
		this.mContext = pContext;
		this.m_listener = listenerr;
		this.m_dismissListener = dismissListener;
		View pView = LayoutInflater.from(pContext).inflate(R.layout.im_popup_selector, null, false);

		m_TvForward = (TextView) pView.findViewById(R.id.im_popup_selector_forward);
		m_TvForward.setOnClickListener(this);

		m_TvCopy = (TextView) pView.findViewById(R.id.im_popup_selector_copy);
		m_TvCopy.setOnClickListener(this);

		m_TvDelete = (TextView) pView.findViewById(R.id.im_popup_selector_delete);
		m_TvDelete.setOnClickListener(this);

		m_TvReply = (TextView) pView.findViewById(R.id.im_popup_selector_reply);
		m_TvReply.setOnClickListener(this);

		m_TvReport = (TextView) pView.findViewById(R.id.im_popup_selector_report);
		m_TvReport.setOnClickListener(this);

		m_DeleteContainer = pView.findViewById(R.id.im_popup_selector_delete_container);

		m_CopyContainer = pView.findViewById(R.id.im_popup_selector_copy_container);

		m_ReplyContainer = pView.findViewById(R.id.im_popup_selector_reply_container);

		m_ForwardContainer = pView.findViewById(R.id.im_popup_selector_forward_container);

		m_ReportContainer = pView.findViewById(R.id.im_popup_selector_report_container);


		m_popup = new PopupWindow();
		m_popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		m_popup.setFocusable(true);
		m_popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		m_popup.setContentView(pView);
		m_popup.setOutsideTouchable(true);
		m_popup.setOnDismissListener(new PopupWindow.OnDismissListener()
		{
			@Override
			public void onDismiss()
			{
				if (null != m_dismissListener)
				{
					m_dismissListener.onDismissListener();
				}
			}
		});
	}

	public void dismiss()
	{
		if (null != m_popup && m_popup.isShowing())
		{
			m_popup.dismiss();
		}
	}

//	public void show()
//	{
//		if (null != m_popup)
//		{
//			m_popup.showAtLocation(showView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//		}
//	}

	public void show(ChatMessage msg, View pView)
	{

		if(msg.getMsgType() == ChatMessage.MSG_TYPE_UCG && msg.getType() == ChatMessage.TYPE_RECEIVE){
			m_ReportContainer.setVisibility(View.VISIBLE);
		}else {
			m_ReportContainer.setVisibility(View.GONE);
		}
		if(msg.getContentType() == ChatMessage.CONTENT_TYPE_ATTACHMENT){
			m_CopyContainer.setVisibility(View.GONE);
		}else {
			m_CopyContainer.setVisibility(View.VISIBLE);
		}


		m_msg = msg;
		int[] location = new int[2];
		pView.getLocationOnScreen(location);
//        Rect tRect = new Rect(location[0], location[1], location[0] + pView.getWidth(), location[1] + pView.getHeight());
		int left = (int) (location[0] + pView.getWidth()/2 - DimentionsUtil.dp2px(78));
		int top = (int) (location[1] - DimentionsUtil.dp2px(50));

		m_popup.showAtLocation(pView, Gravity.NO_GRAVITY, left, top);
	}


	@Override
	public void onClick(View v) {
		int id = v.getId();

		if (m_listener != null) {
			if (id == R.id.im_popup_selector_forward) {
				m_listener.onForward(m_msg);
			} else if (id == R.id.im_popup_selector_copy) {
				m_listener.onCopy(m_msg);
			} else if (id == R.id.im_popup_selector_delete) {
				m_listener.onDelete(m_msg);
			} else if (id == R.id.im_popup_selector_report) {
				m_listener.onReport(m_msg);
			} else if (id == R.id.im_popup_selector_reply) {
				m_listener.onReply(m_msg);
			}
		}

		dismiss();
	}

	public interface IIMSelectorMenu {
		//转发
		void onForward(ChatMessage msg);

		//复制
		void onCopy(ChatMessage msg);

		//删除
		void onDelete(ChatMessage msg);

		//回复
		void onReply(ChatMessage msg);
		//举报

		void onReport(ChatMessage msg);

//        //撤回
//        void onBackendDelete(IMMessage msg);
//
//        //删除全部
//        void onBackendDeleteAll(IMMessage msg);
	}

	public interface OnDismissListener
	{
		void onDismissListener();
	}

}
